package com.itheima.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: dalao
 * @date: 2020/4/9-20:51
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {
    @Reference
    private OrderSettingService orderSettingService;

    @RequestMapping("/setNumberByDate")
    public Result setNumberByDate(@RequestBody OrderSetting orderSetting){
        try {
            orderSettingService.setNumberByDate(orderSetting);
            return Result.OK(MessageConstant.ORDERSETTING_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR(MessageConstant.ORDERSETTING_FAIL);
        }
    }
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile){
        try {
//            通过工具类获取每行数据数组的集合
            List<String[]> list = POIUtils.readExcel(excelFile);
//            判断集合里是否有数据
            if(list!=null&&list.size()>0){
//                如果有数据 , 将数据封装到OrderSetting里
//                创建一个集合
                List<OrderSetting> orderSettingList = new ArrayList<>();
                for (String[] strings : list) {
//     OrderSetting orderSetting =new OrderSetting(new Date(strings[0]), Integer.parseInt(strings[1]));
//                    判断数组长度 , 避免出现无数据情况
                    if(strings.length!=2){
                        continue;
                    }
//                    判断空数据""
                    if(strings[0]!=""&&strings[1]!="") {
//                    创建对象
                        OrderSetting orderSetting = new OrderSetting(new Date(strings[0]), Integer.parseInt(strings[1]));
//                    取出数组元素赋值
//                    orderSetting.setOrderDate(new Date(strings[0]));
//                    orderSetting.setNumber(Integer.parseInt(strings[1]));
//                    添加到集合中
                        orderSettingList.add(orderSetting);
                    }
                }
                orderSettingService.add(orderSettingList);

            }
            return Result.OK(MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.ERROR(MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }

    }

    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(@RequestParam("date") String date){
        try{
           List<Map> List =  orderSettingService.getOrderSettingByMonth(date);
           return Result.OK("",List);
        }catch (Exception e){
            return Result.ERROR("获取失败");
        }
    }
}
